<html>
<head><meta charset="utf-8"><title>union · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html">union</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="173653246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173653246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173653246">(Aug 20 2019 at 12:38)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Does this looks like a bad idea or a terrible idea? <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb</a></p>



<a name="173667641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173667641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173667641">(Aug 20 2019 at 15:21)</a>:</h4>
<p>Well, I've seen worse ideas.</p>



<a name="173793189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173793189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173793189">(Aug 21 2019 at 13:24)</a>:</h4>
<p>Why</p>



<a name="173793222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173793222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173793222">(Aug 21 2019 at 13:25)</a>:</h4>
<p>I mean, that doesn't really buy you anything over a <code>#[repr(C)] union</code></p>



<a name="173793258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173793258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173793258">(Aug 21 2019 at 13:25)</a>:</h4>
<p>If it's just for fun, or to support older Rust toolchains without unions, then sure</p>



<a name="173793264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173793264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173793264">(Aug 21 2019 at 13:25)</a>:</h4>
<p>but since you are using <code>MaybeUninit</code>..</p>



<a name="173794232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173794232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173794232">(Aug 21 2019 at 13:36)</a>:</h4>
<p>My first reaction was similar, but the padding-related ABI headaches w.r.t. unions wouldn't apply the a struct containing a big MaybeUninit.</p>



<a name="173796877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173796877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173796877">(Aug 21 2019 at 14:05)</a>:</h4>
<p>you can add an extra field to the union to avoid those headaches</p>



<a name="173796885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173796885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173796885">(Aug 21 2019 at 14:05)</a>:</h4>
<p>(e.g. <code>()</code> or <code>[u8; size_of::&lt;Self&gt;()]</code>)</p>



<a name="173810148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/173810148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#173810148">(Aug 21 2019 at 16:26)</a>:</h4>
<p>do unions still not support non-Copy types on stable? This would.</p>



<a name="174035030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174035030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174035030">(Aug 24 2019 at 11:15)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> Does this looks like a bad idea or a terrible idea? <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=72153dbf42418f6de937b4ed55dc93eb</a></p>
</blockquote>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> it is certainly interesting. ;)</p>



<a name="174035084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174035084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174035084">(Aug 24 2019 at 11:17)</a>:</h4>
<p>this is some crazy hacking around const limitations for sure</p>



<a name="174035089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174035089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174035089">(Aug 24 2019 at 11:17)</a>:</h4>
<p>but I guess the question is more about the struct thing you are doing</p>



<a name="174035141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174035141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174035141">(Aug 24 2019 at 11:18)</a>:</h4>
<p>I would probably prefer an array-of-<code>MaybeUninit&lt;u8&gt;</code> over a maybeuninit-array, but other than that, as much as I would like to, I cannot find anything inherently wrong with this ;)</p>



<a name="174035297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174035297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174035297">(Aug 24 2019 at 11:24)</a>:</h4>
<p>I view unions as sugar for transmuting memory, and thats what you are implementing here. Nice hack!</p>



<a name="174579787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174579787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174579787">(Aug 30 2019 at 20:26)</a>:</h4>
<p>On top of this I tried implementing compiletime checked unions, keeping track of the current 'selected' type,<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1028dc58599ab7bb757d994e79e8cc4a" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1028dc58599ab7bb757d994e79e8cc4a">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1028dc58599ab7bb757d994e79e8cc4a</a></p>
<p>The test given passes Miri.</p>



<a name="174605682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union/near/174605682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/union.html#174605682">(Aug 31 2019 at 07:01)</a>:</h4>
<blockquote>
<p>On top of this I tried implementing compiletime checked unions, keeping track of the current 'selected' type</p>
</blockquote>
<p>isn't that what <code>enum</code> does?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>